package com.weiwu.weiwustore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiwu.weiwustore.entity.Material;
import com.weiwu.weiwustore.entity.Material_Type;
import com.weiwu.weiwustore.entity.Store;
import com.weiwu.weiwustore.entity.User;
import com.weiwu.weiwustore.mapper.MaterialTypeMapper;
import com.weiwu.weiwustore.mapper.StoreMapper;
import com.weiwu.weiwustore.mapper.MaterialMapper;
import com.weiwu.weiwustore.pojo.*;
import com.weiwu.weiwustore.service.MaterialService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.weiwu.weiwustore.utils.ObjectFormatUtil.toInteger;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    MaterialTypeMapper materialTypeMapper;
    // Spring 框架提供的手动操作事务的两个类
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

    @Override
    public Integer findCount() {
        return materialMapper.selectCount(null);
    }

    @Override
    public List<SelectItem> getmaterialitem() {
        return singlePack(materialTypeMapper.selectList(null));
    }

    @Override
    public List<MaterialListVo> getmaterials(Map<String, Object> map) {
        QueryWrapper<Store> wrapper01 = new QueryWrapper<>();
        String storeid = "", select = "";
        Integer begin = 0, size = 10;
        if (map.containsKey("size")) {
            size = toInteger(map.get("size"));
        }
        if (map.containsKey("page")) {
            Integer page = ObjectFormatUtil.toInteger(map.get("page"));
            begin = (page * size) - size;
        }
        if (map.containsKey("storeid")) {
            if (String.valueOf(map.get("storeid")).contains("仓")) {
                wrapper01.eq("sname", String.valueOf(map.get("storeid")));
                storeid = String.valueOf(storeMapper.selectOne(wrapper01).getId());
            } else {
                storeid = String.valueOf(map.get("storeid"));
            }
        }
        if (map.containsKey("select")) {
            select = String.valueOf(map.get("select"));
        }
        return materialMapper.selectListBymap(begin, size, storeid, select);
    }

    @Override
    public List<MaterialItem> getmaterialitembystoreid(Map<String, Object> map) {
        String storeid = String.valueOf(map.get("storeid"));
        List<MaterialItemVo> list = materialMapper.selectItemBystoreid(storeid);
        return singlePackItem(list);
    }

    @Override
    public Integer materialadd(Map<String, Object> map) {
        boolean isusenew = (boolean) map.get("isusenew");
        String introduce = String.valueOf(map.get("introduce"));
        String quantity = String.valueOf(map.get("quantity"));
        String price = String.valueOf(map.get("price"));
        Double price11 = Double.parseDouble(price);
        String mname = String.valueOf(map.get("mname"));
        Integer storeid = toInteger(map.get("storeid"));
        Material_Type materialType = new Material_Type();
        Integer materialTypeid = 0;
        Material material = new Material(mname, quantity, price11, storeid);
        // 1. 开启事务 (获取事务)
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        if (isusenew) {
            String materialtype = String.valueOf(map.get("materialtype"));
            materialType.setTname(materialtype);
            materialType.setIntroduce(introduce == null ? "暂无" : introduce);
            Integer resT = materialTypeMapper.insert(materialType);
            Material_Type materialType1 = materialTypeMapper.selectOne(new QueryWrapper<Material_Type>().eq("tname", materialtype));
            materialTypeid = materialType1.getId();
            material.setTypeid(materialTypeid);
        } else {
            Integer materialtype = toInteger(map.get("materialtype"));
            material.setTypeid(materialtype);
        }
        Integer resM = materialMapper.insert(material);
        // 2. 自定义提交事务，(或回滚事务)
        transactionManager.commit(transactionStatus);
        return resM;
    }

    @Override
    public MaterialPriceVo getmaterialprice(Map<String, Object> map) {
        Integer id = toInteger(map.get("id"));
        Material material = materialMapper.selectById(id);
        return new MaterialPriceVo(material.getQuantity(),material.getPrice());
    }

    private List<MaterialItem> singlePackItem(List<MaterialItemVo> list) {
        List<String> labels = new ArrayList<>();
        List<MaterialItem> res = new ArrayList<>();
        List<SelectItem> items = new ArrayList<>();
        for (MaterialItemVo m : list) {
            if (!labels.contains(m.getTname())) {
                labels.add(m.getTname());
            }
        }
        for (int i = 0; i < labels.size(); i++) {
            items = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                if (labels.get(i).equals(list.get(j).getTname())) {
                    items.add(new SelectItem(list.get(j).getId().toString(), list.get(j).getMname()));
                }
            }
            res.add(new MaterialItem(labels.get(i), items));
        }
        return res;
    }

    private List<SelectItem> singlePack(List<Material_Type> list) {
        List<SelectItem> res = new ArrayList<>();
        for (Material_Type mt : list) {
            res.add(new SelectItem(String.valueOf(mt.getId()), mt.getTname()));
        }
        return res;
    }
}
